//
//  ViewController.m
//  Emoji表情
//
//  Created by zxy on 2019/5/28.
//  Copyright © 2019 zxy. All rights reserved.
//

#import "ViewController.h"
#import <YYText.h>
#import "EmojiCollectionView.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface ViewController ()<EmojiCollectionViewDelgete>
@property (weak, nonatomic) IBOutlet UIButton *emojiBtn;
//背景view底部约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomCons;
//表情输入框
@property (weak, nonatomic) IBOutlet YYTextView *emojiTextView;
@property(nonatomic,strong)EmojiCollectionView *emojiView;
//保存表情图片对于名称
@property(nonatomic,strong)NSMutableArray *emojiStrArr;
//表情解析器
@property(nonatomic,strong)YYTextSimpleEmoticonParser *emojiParser;
@property(nonatomic,strong)NSMutableDictionary *mapper;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomHeightCons;
@end

@implementation ViewController

-(NSMutableDictionary *)mapper{
    if(!_mapper){
        _mapper = [NSMutableDictionary dictionary];
    }
    return _mapper;
}

-(NSMutableArray *)emojiStrArr{
    if(!_emojiStrArr){
        _emojiStrArr = [NSMutableArray array];
    }
    return _emojiStrArr;
}
-(YYTextSimpleEmoticonParser *)emojiParser{
    if(!_emojiParser){
        _emojiParser = [YYTextSimpleEmoticonParser new];
    }
    return _emojiParser;
}


-(YYTextView *)emojiTextView{
    if(!_emojiTextView){
        YYTextView *tv = [YYTextView new];
        tv.contentSize = CGSizeMake(SCREEN_WIDTH - 90, 30);
        _emojiTextView = tv;
    }
    return _emojiTextView;
}

-(EmojiCollectionView *)emojiView{
    if(!_emojiView){
        _emojiView = [[EmojiCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
        _emojiView.delegate = self;
    }
    return _emojiView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"表情键盘";
    [self.emojiTextView becomeFirstResponder];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark -表情按钮
- (IBAction)emojiBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if(sender.selected){//弹起表情键盘
        self.emojiTextView.inputView = self.emojiView;
        [self.emojiTextView reloadInputViews];
    }else{//弹起系统键盘
        self.emojiTextView.inputView = nil;
        [self.emojiTextView reloadInputViews];
    }
}

#pragma mark -发布按钮
- (IBAction)publicClick:(UIButton *)sender {
    NSLog(@"%@",self.emojiTextView.text);
}

#pragma mark -键盘将要显示
-(void)keyBoardWillShow:(NSNotification *)no{
   //弹出系统键盘
    CGFloat duration = [no.userInfo[UIKeyboardAnimationDurationUserInfoKey]floatValue];
    CGRect rect = [no.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [UIView animateWithDuration:duration animations:^{
        self.bottomCons.constant = rect.size.height;
    }];
    [self.view layoutIfNeeded];
}

#pragma mark -键盘将要隐藏
-(void)keyBoardWillHide:(NSNotification *)no{
    CGFloat duration = [no.userInfo[UIKeyboardAnimationDurationUserInfoKey]floatValue];
    [UIView animateWithDuration:duration animations:^{
        self.bottomCons.constant = 0;
    }];
    [self.view layoutIfNeeded];
}

#pragma mark -获取选择表情对应的图片名称
-(void)selEmojiStr:(NSString *)emojiStr imageName:(NSString *)emojiImgName isClickDeleteButton:(BOOL)isDeleteBtn{
    //点击了删除按钮
    if(isDeleteBtn){
        //删除最后一个字符
        [self.emojiTextView deleteBackward];
        return;
    }
    UIImage *image = [UIImage imageNamed:emojiImgName];
    [self.mapper setValue:image forKey:emojiStr];
    //设置表情解析器，如dic[@"[微笑]"] = 微笑图片，解析的时候，文本含有[微笑]的，会自动解析成表情微笑图片
    self.emojiParser.emoticonMapper = self.mapper;
    //设置表情光标自动移动到最后一个表情后面
    self.emojiTextView.selectedRange = NSMakeRange(0, self.emojiTextView.text.length);
    self.emojiTextView.text = [NSString stringWithFormat:@"%@%@",self.emojiTextView.text,emojiStr];
    self.emojiTextView.font = [UIFont systemFontOfSize:17];
    self.emojiTextView.textParser = self.emojiParser;
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
